#ifndef ASYNCEXPORTWORKER_H
#define ASYNCEXPORTWORKER_H

#include <QObject>
#include <QThread>
#include <QTimer>
#include <QMutex>
#include <QWaitCondition>
#include <QList>
#include <QImage>
#include "unifiedfileinfo.h"
#include "processingsettings.h"
#include "simpleprogressnotifier.h"

class MergedExportManager;
class UnifiedPageRenderer;

/**
 * 异步导出工作线程
 * Martin Fowler重构：Replace Synchronous Processing with Asynchronous
 * 解决主线程阻塞问题
 */
class AsyncExportWorker : public QObject
{
    Q_OBJECT

public:
    struct ExportTask {
        QList<UnifiedFileInfo> files;
        ProcessingSettings settings;
        QString outputPath;
        QString taskId;
        QList<QImage> displayedPages;  // 主窗口当前显示的页面（确保所见即所得）
    };

    explicit AsyncExportWorker(QObject* parent = nullptr);
    ~AsyncExportWorker();

    // 任务管理
    void submitTask(const ExportTask& task);
    void cancelCurrentTask();
    void pauseCurrentTask();
    void resumeCurrentTask();

    // 状态查询
    bool isProcessing() const;
    bool isPaused() const;
    QString getCurrentTaskId() const;

    // 组件设置
    void setExportManager(MergedExportManager* manager);
    void setPageRenderer(UnifiedPageRenderer* renderer);

    // 进度通知器
    SimpleProgressNotifier* getProgressNotifier() { return m_progressNotifier; }

signals:
    void taskStarted(const QString& taskId);
    void taskCompleted(const QString& taskId, const QString& outputPath);
    void taskFailed(const QString& taskId, const QString& error);
    void taskCancelled(const QString& taskId);

public slots:
    void processNextTask();

private slots:
    void onExportCompleted(const QString& outputPath);
    void onExportFailed(const QString& error);
    void onProgressTimer();

private:
    // 核心处理逻辑
    void processTask(const ExportTask& task);
    void loadPagesAsync(const ExportTask& task);
    void performExport(const ExportTask& task, const QList<QImage>& pages);

    // 页面加载
    QList<QImage> loadAllPagesForFile(const UnifiedFileInfo& fileInfo);
    QImage loadPageFromFile(const QString& filePath, int pageIndex);

    // PDF生成
    void generateRealPDF(const QList<QImage>& pages, const QString& outputPath, const ProcessingSettings& settings);
    void generatePDFWithUnifiedRenderer(const QList<QImage>& pages, const QString& outputPath, const ProcessingSettings& settings);

    // 页面获取方法
    QList<QImage> getCurrentDisplayedPages();
    QList<QImage> processFilesToPages(const QList<UnifiedFileInfo>& files);

    // 暂停/恢复控制
    void checkPauseCondition();
    void wakeFromPause();

    // 状态管理
    MergedExportManager* m_exportManager;
    UnifiedPageRenderer* m_pageRenderer;
    SimpleProgressNotifier* m_progressNotifier;

    // 任务队列和状态
    QList<ExportTask> m_taskQueue;
    ExportTask m_currentTask;
    bool m_isProcessing;
    bool m_isPaused;
    bool m_isCancelled;

    // 线程同步
    mutable QMutex m_mutex;
    QWaitCondition m_pauseCondition;

    // 进度管理
    QTimer* m_progressTimer;
    int m_totalPages;
    int m_processedPages;
    int m_currentFileIndex;
    int m_currentPageIndex;

    // 辅助方法
    QString generateTaskId() const;
    void cleanup();
};

#endif // ASYNCEXPORTWORKER_H